#include "loginwgt.h"
#include<QMouseEvent>
#include<QPushButton>
#include<QVBoxLayout>
#include<QLabel>
#include<QHBoxLayout>
#include<QLineEdit>
#include"styleloader.h"
#include"ICMS.h"
#include"AppConfig.h"
LoginWgt::LoginWgt(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(300,400);
    initLayout();
    StyleLoader::getInstance()->loadStyle(":/qss/main.css",this);
}

void LoginWgt::slot_Operator(const REQUESTTYPE type, bool result)
{
    switch (type) {
    case REGISTER:
        handleRegister(result);
        break;
    case LOGIN:
        handleRegister(result);
        break;
    default:
        break;
    }
}

void LoginWgt::slot_MediaInfo(const QVector<QVariantMap> mediainfo)
{
    //
    if(mediainfo.isEmpty())
    {
        ICMS* icms = new ICMS();
        icms->show();
        hide();
    }
}

void LoginWgt::mousePressEvent(QMouseEvent *event)
{
    if(!qobject_cast<QPushButton*>(childAt(event->pos())))
    {
        isPressed = true;
        point = event->globalPos() - this->frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}


void LoginWgt::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton && isPressed)
    {
        if(!qobject_cast<QPushButton*>(childAt(event->pos())))
        {
            move(event->globalPos()-point);
        }
    }
    QWidget::mouseMoveEvent(event);
}
void LoginWgt::mouseReleaseEvent(QMouseEvent *event){
    if(event->button() == Qt::LeftButton)
    {
        isPressed = false;
    }
    QWidget::mouseReleaseEvent(event);
}

void LoginWgt::handleLogin(bool result)
{
    if(result)
    {
        AppConfig* app = AppConfig::instance();
        HttpHelper* httpHelper = HttpHelper::instance();
        QVector<QVariantMap> infos = app->getVideoFiles();
        for(const QVariantMap& video : infos)
        {
            if(video.contains("mediaUrl") && video.contains("mediaTitle"))
            {
                QString mediaUrl = video["mediaUrl"].toString();
                QString mediaTitle = video["mediaTitle"].toString();
                httpHelper->slot_AddmediaInfo(video);
                qDebug() <<"媒体文件: "<<mediaTitle <<"路径,"<<mediaUrl;
            }
            else
            {
                qDebug()<<"缺少必要字段";
            }
        }
        httpHelper->slot_GetmediaInfo();
    }
    else
    {
        qDebug()<<"登陆失败";
    }
}

void LoginWgt::handleRegister(bool result)
{
    if(result)
    {
        stackedWgt->setCurrentWidget(loginWgt);
    }
    else
    {
        qDebug() << "注册失败";
    }
}

void LoginWgt::initLayout()
{
    //著布局
    QVBoxLayout* mainlayout = new QVBoxLayout(this);

    QWidget* headerWgt = new QWidget(this);
    headerWgt->setFixedSize(300,150);
    headerWgt->setWindowFlag(Qt::FramelessWindowHint);
    headerWgt->setAttribute(Qt::WA_StyledBackground);

    QLabel* iconLabel = new QLabel(headerWgt);
    iconLabel->setAlignment(Qt::AlignCenter);
    QPushButton* closeBth = new QPushButton(headerWgt);
    iconLabel->setFixedSize(80,80);
    closeBth->setFixedSize(30,30);

    iconLabel->setPixmap(QPixmap("path"));

    iconLabel->setScaledContents(true);

    connect(closeBth,&QPushButton::clicked,this,[this](){
        close();
    });

    QVBoxLayout* headerlayout = new QVBoxLayout(headerWgt);
    headerlayout->setContentsMargins(0,0,0,0);
    headerlayout->setSpacing(0);


    QHBoxLayout* toplapout = new QHBoxLayout();
    toplapout->setContentsMargins(0,0,0,0);
    toplapout->addStretch();
    toplapout->addWidget(closeBth);
    headerlayout->addLayout(toplapout);

    headerlayout->addStretch();

    QHBoxLayout* iconlayout = new QHBoxLayout();
    iconlayout->setContentsMargins(0,0,0,0);
    iconlayout->addStretch();
    iconlayout->addWidget(iconLabel);
    iconlayout->addStretch();
    headerlayout->addLayout(iconlayout);

    headerWgt->setLayout(headerlayout);

    //stack
    stackedWgt = new QStackedWidget(this);
    stackedWgt->setFixedSize(300,250);
    stackedWgt->setWindowFlag(Qt::FramelessWindowHint);
    stackedWgt->setAttribute(Qt::WA_StyledBackground);

    mainlayout->setSpacing(0);
    mainlayout->addWidget(headerWgt);
    mainlayout->addWidget(stackedWgt);
    mainlayout->setContentsMargins(0,0,0,0);


    loginWgt = new QWidget(stackedWgt);
    registerWgt = new QWidget(stackedWgt);

    loginWgt->setFixedSize(300,250);
    loginWgt->setWindowFlag(Qt::FramelessWindowHint);
    loginWgt->setAttribute(Qt::WA_StyledBackground);

    registerWgt->setFixedSize(300,250);
    registerWgt->setWindowFlag(Qt::FramelessWindowHint);
    registerWgt->setAttribute(Qt::WA_StyledBackground);

    //添加登录窗口子控件
    QLineEdit* account = new QLineEdit(loginWgt);
    QLineEdit* passwd = new QLineEdit(loginWgt);
    QPushButton* loginBtn = new QPushButton("登录",loginWgt);
    QPushButton* registerBtn = new QPushButton("没有账号？注册",loginWgt);

    account->setFixedSize(250,40);
    passwd->setFixedSize(250,40);
    loginBtn->setFixedSize(250,40);
    registerBtn->setFixedSize(250,40);

    account->setPlaceholderText("username");
    passwd->setPlaceholderText("passwd");
    passwd->setEchoMode(QLineEdit::Password);

    //登录窗口控制布局
    QVBoxLayout* loginlayout = new QVBoxLayout(loginWgt);
    loginlayout->setAlignment(Qt::AlignCenter);
    loginlayout->setSpacing(10);
    loginlayout->addWidget(account);
    loginlayout->addWidget(passwd);
    loginlayout->addStretch();
    loginlayout->addWidget(loginBtn);
    loginlayout->addWidget(registerBtn);
    loginlayout->addStretch();
    loginlayout->setContentsMargins(0,0,0,0);
    loginWgt->setLayout(loginlayout);
    //注册子控件
    QLineEdit* reusername = new QLineEdit(registerWgt);
    QLineEdit* repasswd = new QLineEdit(registerWgt);
    QPushButton* startRegisterBtn = new QPushButton("开始注册",registerWgt);
    QPushButton* reloginBtn = new QPushButton("返回登陆",registerWgt);

    connect(loginBtn,&QPushButton::clicked,this,[=](){
        QVariantMap infoMap;
        QString username = account->text();
        QString userpasswd = passwd->text();
        if(username.isEmpty() && !userpasswd.isEmpty())
        {
            infoMap["userName"] = username;
            infoMap["password"] = userpasswd;
            infoMap["nickName"] = "text";
            HttpHelper::instance()->slot_Login(infoMap);
        }
        else
        {
            qDebug() <<"账号密码不能为空";
        }
    });
    connect(startRegisterBtn,&QPushButton::clicked,this,[=](){
        QVariantMap infoMap;
        QString username = reusername->text();
        QString userpasswd = repasswd->text();
        if(username.isEmpty() && !userpasswd.isEmpty())
        {
            infoMap["userName"] = username;
            infoMap["password"] = userpasswd;
            infoMap["nickName"] = "text";
            HttpHelper::instance()->slot_Register(infoMap);
        }
        else
        {
            qDebug() <<"账号密码不能为空";
        }
    });

    connect(reloginBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(loginWgt);
    });

    reusername->setFixedSize(250,40);
    repasswd->setFixedSize(250,40);
    startRegisterBtn->setFixedSize(250,40);
    reloginBtn->setFixedSize(250,40);


    reusername->setPlaceholderText("reusername");
    repasswd->setPlaceholderText("repasswd");
    repasswd->setEchoMode(QLineEdit::Password);


    QVBoxLayout* registerlayout = new QVBoxLayout(registerWgt);
    registerlayout->setAlignment(Qt::AlignCenter);
    registerlayout->setSpacing(10);
    registerlayout->addWidget(reusername);
    registerlayout->addWidget(repasswd);
    registerlayout->addStretch();
    registerlayout->addWidget(startRegisterBtn);
    registerlayout->addWidget(reloginBtn);
    registerlayout->addStretch();
    registerlayout->setContentsMargins(0,0,0,0);
    registerWgt->setLayout(registerlayout);


    stackedWgt->addWidget(loginWgt);
    stackedWgt->addWidget(registerWgt);

    connect(registerBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(registerWgt);
    });

    stackedWgt->setCurrentWidget(loginWgt);

    this->setLayout(mainlayout);


   StyleLoader::getInstance()->loadStyle(":/qss/main.css",this);




};
